import TableLyout from '@/components/commonPublic/tableLyoutDetail.vue'
import TableData from "@/components/commonPublic/el-table"; //主页面列表
import CustomDialog from "@/components/BehaViorManage/workReportWeekText/index.vue";
import {
    getJobbriefingdataPageList, //列表页接口
    deleteJobbriefingdataByfId, //监理、实验室删除
    submitJobbriefingdataByfId, //监理、试验室提交
} from "@/api/request.js";
import publicForm from "@/components/QualityGridManage/fromCom.vue"; //弹框form
import tree from "@/components/QualityGridManage/tree"; //左侧机构树
import elBreadcrumb from "@/components/public/el-breadcrumb.vue";
import formJs from "./regexDetail.js"; //新增弹框配置项
export default {
    name: "workReportWeekText",
    data() {
        return {
            name: "workReportWeekText",
            isLock: false,
            projectId: null,
            projectName: null,
            loading: false,
            btnObj: [], //按钮
            queryCriteriaData: {}, //分页
            searchObj: formJs.searchObj, //搜索条件
            table_config: formJs.table_config, //列表
            radio: "待办",
            // 列表
            getList: {
                fCompanyid: "", //分子公司或建设项目id
                keyType: "", //计划类型
                fStatus: "", //完成情况(待审核,执行中,已打回,已完成)
                companyid: "", //项目部id
                page: 1, // 当前页码
                pageSize: 20, // 每页显示条数
                userId: "", // 查询待办或已办时传 列表页查询不传
                modelType: "0" // 0-待办 1-已办 传2 表示列表页查询
            },
            isShow: false, //是否显示弹框
            status: null, //按钮状态1新增 3编辑
            statusNum: null,
            dataFid: null, //列表详情id
            currentRow: null, //列表详情
            activeIndex: "1",
            fId: null, //当前数据fId
            tableData: [],
            companyId: null,
            currentRowId: "",
        };
    },
    components: {
        TableLyout,
        publicForm,
        TableData,
        tree,
        elBreadcrumb,
        CustomDialog
    },
    mounted() {
        this.userInfo = JSON.parse(sessionStorage.getItem("userInfo"));
        // console.log("this.userInfo", this.userInfo);
        this.projectId = this.userInfo.projectId
        // this.projectName = this.userInfo.projectId
        this.projectName = this.userInfo.projectName;

        this.getList.userId = this.userInfo.userId
        this.getList.fCompanyid = this.userInfo.companyId
        this.yj_searchForm(this.getList);
        this.btnRole(this.name)
    },
    methods: {
        refreshList() { //刷新列表
            this.$nextTick(() => {
                this.yj_searchForm(this.getList);
            })
        },
        closeDialog() {
            this.isShow = false;
        },
        btnRole(name) {
            if (this.userInfo.userId != 'System') {
                var btnList = [];
                var ModuleList = JSON.parse(sessionStorage.getItem("ModuleList"));
                if (ModuleList.length > 0) {
                    ModuleList.map((item) => {
                        if (item.fEncode == name) {
                            btnList = item.modulebuttonList;
                        }
                    });
                    // disabled
                    btnList.map(item => {
                        item['disabled'] = false
                    })
                    this.btnObj = btnList
                }
            } else {
                this.btnObj = formJs.btnObj
            }
            // console.log(this.btnObj);
        },
        getBtnName(name, status) {
            this.statusNum = status
            this.yj_edit(status)
        },
        // 重置按钮
        modelType(e) {
            this.fId = "";
            if (e == "待办") {
                this.getList.modelType = "0";
            } else {
                this.getList.modelType = "1";
            }
            this.yj_searchForm(this.getList);
        },
        // 新增/编辑
        yj_edit(num) {
            if (num == 1) {
                this.status = [true, false]
                // 新增 项目公司、建设单位  
                if (this.userInfo.companyNature == '建设单位' || this.userInfo.companyNature == '分子公司') {
                    this.editText();
                } else {
                    this.$message.warning("该用户无法新增");
                }
            } else if (num == 2) { //编辑
                if (this.dataFid != null) {
                    this.status = [true, false]
                    // 编辑
                    this.editText();
                } else {
                    this.$message.warning("请先选择需要编辑的数据");
                }
            } else if (num == 3) { //提交
                if (this.dataFid != null) {
                    // 提交
                    this.yj_submit();
                } else {
                    this.$message.warning("请先选择需要提交的数据");
                }
            } else if (num == 4) {
                if (this.dataFid != null) {
                    this.yj_delete()

                } else {
                    this.$message.warning("请先选择需要删除的数据");
                }
            }

        },
        editText() {
            this.isShow = true;
        },
        // 提交
        yj_submit() {
            if (this.dataFid != "" && this.dataFid != null) {
                this.$confirm("是否继续提交?", "提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning",
                })
                    .then(() => {
                        this.loading = true;
                        this.isLock = true
                        submitJobbriefingdataByfId({
                            fId: this.dataFid,
                            userId: this.userInfo.userId, //登录id
                            userName: this.userInfo.realName //登录名
                        })
                            .then((res) => {
                                if (res.errcode == 0) {
                                    this.isLock = false
                                    this.loading = false;
                                    this.yj_searchForm(this.getList);
                                    this.dataFid = null
                                    this.$message.success("提交成功");
                                } else {
                                    this.isLock = false
                                    this.$message.error("提交失败", res.msg);
                                    this.loading = false;
                                    return false;
                                }
                            })
                            .catch(() => {
                                this.loading = false;
                                console.log("提交失败");
                            });
                    })
                    .catch(() => {
                        this.$message("取消提交");
                    });
            } else {
                return this.$message.error("请先选择需要提交的数据");
            }
        },

        // 删除
        yj_delete() {
            if (this.dataFid != "" && this.dataFid != null) {
                this.$confirm("您是否要永久删除此数据?", "提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning",
                })
                    .then(() => {
                        this.isLock = true
                        this.loading = true;
                        deleteJobbriefingdataByfId({
                            fId: this.dataFid
                        })
                            .then((res) => {
                                if (res.errcode == 0) {
                                    this.loading = false;
                                    this.dataFid = null
                                    this.isLock = false
                                    this.yj_searchForm(this.getList);
                                    this.$message.success("删除成功");
                                } else {
                                    this.isLock = false
                                    this.$message.error("删除失败", res.msg);
                                    this.loading = false;
                                }
                            })
                            .catch(() => {
                                console.log("删除失败");
                                this.loading = false;
                            });
                    })
                    .catch(() => {
                        this.$message("取消删除");
                    });
            } else {
                return this.$message.error("请先选择需要删除的数据");
            }
        },
        // 查看
        // 点击查看
        yj_viewForm(row, label) {

            if (label == "信息状态") {
                this.statusNum = 7
                this.isShow = true;
                this.currentRow = row
                this.status = [true]
            }
        },



        // 点击当前行变色
        rowChange(val) {

            this.currentRow = val
            this.dataFid = val.fId;

        },


        // 获取列表数据
        yj_searchForm(data) {
            var obj = {
                fSubunitid: data.fCompanyid, //左侧选中组织机构id
                page: data.page, // 当前页码
                pageSize: data.pageSize, // 每页显示条数
                userId: data.userId, // 查询待办或已办时传(当前接口仅限编辑页)
                modelType: data.modelType // 0-待办 1-已办 
            }

            this.loading = true;
            var that = this;
            getJobbriefingdataPageList(obj)
                .then((res) => {
                    if (res.errcode == 0) {
                        that.loading = false;
                        // console.log(888, res.data);
                        that.tableData = res.data.pageInfo.list;
                        this.queryCriteriaData = res.data.pageInfo;
                    } else {
                        this.$message.error("列表数据获取失败", res.msg);
                        this.loading = false;
                    }
                })
                .catch((error) => {
                    this.$message.error("列表数据 接口出错", error.msg);
                    this.loading = false;
                });
        },
        // 下面图表

        // 点击当前行
        getRowInfo(e) {
            this.equipmentDetailId = e.F_Id;
        },


        handleSelect(key) {
            this.activeIndex = key;
        },
    },
};